package com.zfei.gfb.dao;

import com.zfei.gfb.api.domain.EventTier;
import com.zfei.gfb.api.domain.EventTuple;
import com.zfei.gfb.device.RedisNames;
import com.zfei.gfb.model.TalkEventEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author fgh
 */
@Service
@CacheConfig(cacheNames = RedisNames.CACHE)
public class TalkEventDao {

    private static final Logger logger = LoggerFactory.getLogger(TalkEventDao.class);

    @Resource
    private TalkEventMapper talkEventMapper;


    public int save(TalkEventEntity pojo) {
        return talkEventMapper.insertSelective(pojo);
    }

    public int update(TalkEventEntity pojo) {
        return talkEventMapper.update(pojo);
    }

    public TalkEventEntity get(String eventId) {
        TalkEventEntity entity = talkEventMapper.findByEventId(eventId);
        if (entity == null) {
            logger.info("event cache null,eventId={}", eventId);
        }
        return entity;
    }

    public List<TalkEventEntity> list(Collection<String> eventIds) {

        return talkEventMapper.findByEventIdList(eventIds);
    }

    public List<EventTuple> getClaim(String account, Collection<String> parents) {

        return list(account, EventTier.CLAIMS, null, null, parents);
    }

    public List<EventTuple> getBrowse(String account, Collection<String> parents) {

        return list(account, EventTier.BROWSES, null, null, parents);
    }

    public List<EventTuple> getCollect(String account, Collection<String> parents) {

        return list(account, EventTier.COLLECTORS, null, null, parents);
    }

    public List<EventTuple> list(String account,
                                 Collection<Integer> eventTiers,
                                 Collection<Integer> eventTypes,
                                 Collection<Integer> privileges,
                                 Long minTime, Long maxTime, Integer offset, Integer limit,
                                 Integer orderType, Integer validFlag) {
        return talkEventMapper.findByAccount(account, eventTiers, eventTypes, privileges, minTime, maxTime,
                offset, limit, orderType, validFlag);
    }

    public List<EventTuple> list(Collection<String> accounts,
                                 Collection<Integer> eventTiers,
                                 Collection<Integer> eventTypes,
                                 Collection<Integer> privileges,
                                 Long minTime, Long maxTime, Integer offset, Integer count,
                                 Integer orderType, Integer validFlag) {
        return talkEventMapper.findByAccounts(accounts, eventTiers, eventTypes, privileges, minTime, maxTime,
                offset, count, orderType, validFlag);
    }

    public int count(String account,
                     Collection<Integer> eventTiers,
                     Collection<Integer> eventTypes,
                     Collection<Integer> privileges,
                     Long minTime, Long maxTime, Integer validFlag) {
        return talkEventMapper.countByAccount(account, eventTiers, eventTypes, privileges, minTime, maxTime, validFlag);
    }

    public List<EventTuple> list(String account,
                                 Collection<Integer> eventTiers,
                                 Collection<Integer> eventTypes,
                                 Collection<Integer> privileges,
                                 Collection<String> parents) {
        return talkEventMapper.findByAccountAndParent(account, eventTiers, eventTypes, privileges, parents);
    }

    public List<EventTuple> list(String parent, Collection<Integer> eventTiers, Long minTime, Long maxTime,
                                 Integer offset, Integer count, Integer orderType) {
        return talkEventMapper.findByParent(parent, eventTiers, minTime, maxTime, offset, count, orderType);
    }

    public List<EventTuple> listAccount(String account,
                                        Collection<Integer> eventTiers,
                                        Collection<Integer> eventTypes,
                                        Collection<Integer> privileges,
                                        Long minTime, Long maxTime, Integer offset, Integer count,
                                        Integer orderType, Integer validFlag) {
        return talkEventMapper.findByAccount(account, eventTiers, eventTypes, privileges, minTime, maxTime,
                offset, count, orderType, validFlag);
    }

    public int countByAccountAndContent(String parent, Collection<Integer> eventTiers,
                                        Long minTime, Long maxTime,
                                        String content, Collection<Integer> privileges,
                                        String spam) {
        return talkEventMapper.countByAccountAndContent(parent, eventTiers, minTime, maxTime, content, privileges, spam);
    }

    public List<EventTuple> findByAccountAndContent(String parent, Collection<Integer> eventTiers,
                                                    Long minTime, Long maxTime,
                                                    Integer offset, Integer limit,
                                                    String content, Collection<Integer> privileges,
                                                    String spam, Integer orderType) {
        return talkEventMapper.findByAccountAndContent(parent, eventTiers, minTime, maxTime, offset, limit, content, privileges, spam, orderType);

    }

    public void updatePrivilege(List<String> eventIds, Integer status) {
        for (String eventId : eventIds) {
            talkEventMapper.updatePrivilege(eventId, status);
        }
    }

    public void updateReply(List<String> eventIds) {
        talkEventMapper.updateReply(eventIds);
    }
}
